<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>

  </style>
</head>
<body>
  <h2>判断子串个数</h2>
  <label>请输入一个字符串:</label><br><input type="text" placeholder="" class="text1"><br>
  <label >请输入一个字串:</label><br><input type="text" class="text2">
  <button>提交</button><br>
  <label>字串出现的次数为:</label><br>

  <script>
    const text1 = document.querySelector('.text1')
    const text2 = document.querySelector('.text2')
    const button = document.querySelector('button')
    const result = document.querySelector('label:nth-of-type(3)')
    button.addEventListener('click', function(){
      let inputText1 = text1.value
      let inputText2 = text2.value
      let count = strCount(inputText1, inputText2)
      result.innerText = `字串出现的次数为: ${count}`
    })
     function strCount(mainStr, secStr){
        let cnt = 0
        let index = mainStr.indexOf(secStr)
        while(index !== -1){
          cnt++
          index = mainStr.indexOf(secStr, index + 1)
        }
        return cnt
      }
  </script>
</body>
</html>